package com.six666.medicaldevicetestingsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.six666.medicaldevicetestingsystem.dto.Equipment_Scheme_DTO;
import com.six666.medicaldevicetestingsystem.entity.EquipmentList;
import com.six666.medicaldevicetestingsystem.entity.EquipmentScheme;
import com.six666.medicaldevicetestingsystem.mapper.EquipmentList_Mapper;
import com.six666.medicaldevicetestingsystem.mapper.EquipmentScheme_Mapper;
import com.six666.medicaldevicetestingsystem.service.Equipment_Scheme_Service;
import com.six666.medicaldevicetestingsystem.service.Equipment_Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class Equipment_Service_Impl extends ServiceImpl<EquipmentList_Mapper, EquipmentList> implements Equipment_Service {
    @Autowired
    EquipmentScheme_Mapper scheme_mapper;
    @Autowired
    Equipment_Scheme_Service equipment_scheme_service;
    @Override
    public List<EquipmentList> get_equipment_by_project(int project_id) {
        QueryWrapper<EquipmentList> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("projectid",project_id);
        return list(queryWrapper);
    }

    @Override
    public List<Equipment_Scheme_DTO> get_scheme_by_equipment(int machine_id) {
        QueryWrapper<EquipmentScheme> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("equipmentid",machine_id);
        List<EquipmentScheme> res = scheme_mapper.selectList(queryWrapper);
        List<Equipment_Scheme_DTO> dtos=new ArrayList<>();
        for(EquipmentScheme scheme:res){
            Equipment_Scheme_DTO dto=equipment_scheme_service.get_equipment_scheme_by_id(scheme.getSchemeid());
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public Boolean add_equip(int project_id, String equipment_name) {
        QueryWrapper<EquipmentList> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("projectid",project_id);
        queryWrapper.eq("equipmentname",equipment_name);
        if (!list(queryWrapper).isEmpty()){
            return false;
        }
        EquipmentList equipment=new EquipmentList();
        equipment.setProjectid(project_id);
        equipment.setEquipmentname(equipment_name);
        return save(equipment);
    }

    @Override
    public int get_project_id_by_equipment_id(int equipment_id) {
        QueryWrapper<EquipmentList>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("equipmentid",equipment_id);
        EquipmentList equipment=getOne(queryWrapper);
        return equipment.getProjectid();
    }
}
